<template>
  <div class="order">
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 画布 -->
    <el-card>
      <!-- 搜索框和添加商品按钮 -->
      <el-row>
        <el-col :span="8">
          <el-input
            placeholder="请输入内容"
            v-model="queryInfo.query"
            @keyup.enter.native="getOrderList"
            clearable
            @clear="getOrderList"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="getOrderList"
            ></el-button>
          </el-input>
        </el-col>
      </el-row>
      <!-- 订单表格渲染
       -->
      <el-table
        :data="ordersList"
        style="width: 100%;margin:20px 0;"
        border
        stripe
        :header-cell-style="{
          background: '#F5F5F5',
          color: '#909399',
          fontSize: '14px',
          fontWeight: '400'
        }"
      >
        <!-- 索引列type = 'index' -->
        <el-table-column type="index" width="50"></el-table-column>
        <el-table-column prop="order_number" label="订单编号" width="250">
        </el-table-column>
        <el-table-column prop="order_price" label="订单价格（元）">
        </el-table-column>
        <el-table-column prop="pay_status" label="是否付款">
          <template slot-scope="scope">
            <el-tag
              type="success"
              size="medium"
              v-if="scope.row.pay_status === '1'"
            >
              已付款
            </el-tag>
            <el-tag type="danger" size="medium" v-else>未付款</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="is_send" label="是否发货"> </el-table-column>
        <el-table-column prop="create_time" label="下单时间" width="180">
          <template slot-scope="scope">
            {{ scope.row.create_time | dateFormat }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="150">
          <template slot-scope="scope">
            <el-tooltip
              effect="dark"
              content="修改订单地址"
              placement="top"
              :enterable="false"
            >
              <el-button
                type="primary"
                icon="el-icon-edit"
                size="mini"
                @click="showChangeAddressDialog(scope.row)"
              ></el-button>
            </el-tooltip>

            <el-tooltip
              effect="dark"
              content="物流信息"
              placement="top"
              :enterable="false"
            >
              <el-button
                type="success"
                icon="el-icon-location"
                size="mini"
                @click="showProgressBox"
              ></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页条 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        background
      >
      </el-pagination>
    </el-card>

    <!-- 修改订单地址对话框 -->
    <el-dialog
      title="修改地址"
      :visible.sync="changeAddressDialogVisible"
      width="50%"
      @close="changeAddressDialogClosed"
    >
      <el-form
        :model="addressForm"
        :rules="addressFormRules"
        ref="addressFormRef"
        label-width="100px"
      >
        <el-form-item label="省市区/县" prop="addressPro">
          <el-cascader
            v-model="addressForm.addressPro"
            :options="cityData"
            :props="cascaderProps"
            clearable
          ></el-cascader>
        </el-form-item>
        <el-form-item label="详细地址" prop="detailAddress">
          <el-input v-model="addressForm.detailAddress"></el-input>
        </el-form-item>
      </el-form>
      <!-- 对话框底部按钮 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="changeAddressDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="changeAddressDialogVisible = false"
          >确 定</el-button
        >
      </span>
    </el-dialog>

    <!-- 物流进度信息对话框 -->
    <el-dialog
      title="物流进度"
      :visible.sync="progressDialogVisible"
      width="50%"
    >
      <!-- 时间线组件显示物流进度 -->
      <el-timeline>
        <el-timeline-item
          v-for="(item, index) in progressInfo"
          :key="index"
          :timestamp="item.time"
        >
          {{ item.context }}
        </el-timeline-item>
      </el-timeline>
    </el-dialog>
  </div>
</template>

<script>
import cityData from './cityData'
export default {
  name: 'order',
  components: {},
  data() {
    return {
      queryInfo: {
        query: '',
        pagenum: 1, // 页码
        pagesize: 10 // 页的大小
      },
      total: 0,
      ordersList: [],
      //   控制修改地址对话框的显示
      changeAddressDialogVisible: false,
      //   地址表单
      addressForm: {
        addressPro: [], // 省市区
        detailAddress: '' // 详细地址
      },
      addressFormRules: {
        addressPro: [
          { required: true, message: '请选择省市区/县', trigger: 'blur' }
        ],
        detailAddress: [
          { required: true, message: '请输入详细地址', trigger: 'blur' },
          { min: 2, max: 30, message: '长度在 2 到 30 个字符', trigger: 'blur' }
        ]
      },
      cityData,
      cascaderProps: {
        expandTrigger: 'hover'
      },
      // 物流信息对话框的显示
      progressDialogVisible: false,
      //   物流信息
      progressInfo: []
    }
  },
  created() {
    this.getOrderList()
  },
  mounted() {},
  methods: {
    async getOrderList() {
      const { data: res } = await this.$http.get('orders', {
        params: this.queryInfo
      })
      if (res.meta.status !== 200) return this.$http.error(res.meta.msg)
      //   this.$message.success('获取订单列表成功！')
      console.log('orderList==>', res)
      this.ordersList = res.data.goods
      this.total = res.data.total
    },
    // 处理分页大小改变，重新请求
    handleSizeChange(newSize) {
      this.queryInfo.pagesize = newSize
      this.getOrderList()
    },
    // 处理当前页码改变，重新请求
    handleCurrentChange(newNum) {
      this.queryInfo.pagenum = newNum
      this.getOrderList()
    },
    // 显示修改地址对话框
    showChangeAddressDialog() {
      this.changeAddressDialogVisible = true
      //   修改地址请求，暂无详细接口
    },
    // 清空验证填写状态
    changeAddressDialogClosed() {
      this.$refs.addressFormRef.resetFields()
    },
    // 显示物流进度信息对话框
    async showProgressBox() {
      // 获取物流信息
      //   测试单号id:1106975712662
      const { data: res } = await this.$http.get(
        'https://www.liulongbin.top:8888/api/private/v1/kuaidi/1106975712662'
      )
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      //   this.$message.success('获取物流信息成功')
      this.progressInfo = res.data
      console.log(this.progressInfo)
      this.progressDialogVisible = true
    }
  }
}
</script>
<style scoped>
/* @import url(); 引入公共css类 */
.el-cascader {
  width: 100%;
}
</style>
<style>
/* 解决cascader溢出的问题 */
.el-cascader-panel {
  height: 300px;
}
.el-timeline-item__content {
  line-height: 20px;
}
</style>
